<?php
/**
 * MatchDart! is a darts event ranking table component for joomla
 * 
 * @version		2.0.1 | $Id: season.php 116 2009-10-12 14:08:54Z mwi.nor $
 * @package		MatchDart!
 * @subpackage	Admin
 * @copyright	Copyright (C) 2005 - 2009 Marco Wilde. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * @author		Marco Wilde, <mailto://info@matchdart.de>
 * @link		http://www.matchdart.de
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

class MatchdartModelSeason extends JModel
{
    protected $_data = null;
    protected $_id = null;

    public function __construct()
    {
        parent::__construct();

        $array = JRequest::getVar('cid',  0, '', 'array');
        $this->setId((int)$array[0]);
    }

    public function setId($id)
    {
        // Set id and wipe data
        $this->_id        = $id;
        $this->_data    = null;
    }

    public function getData()
    {
        $app = &JFactory::getApplication();

        // Load the data
        if(empty( $this->_data )) {
            $query = ' SELECT * FROM #__md_seasons '.
                 '  WHERE SeasonID = '.$this->_id;
            $this->_db->setQuery( $query );
            $this->_data = $this->_db->loadObject();
        }
        if (!$this->_data) {
            $this->_data = $this->getTable('seasons');
            $this->_data->id = 0;
        }
        return $this->_data;
    }

    public function store()
    {
        $row =& $this->getTable('seasons');
        $data = JRequest::get( 'post' );
        
        if (!isset($data->SeasonPublish)) {
        	$data->SeasonPublish = 0;
        }  

        if (!isset($data->SeasonDefault)) {
        	$data->SeasonDefault = 0;
        }  
        

        // Bind the form fields to the hello table
        if (!$row->bind($data)) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        // Make sure the hello record is valid
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        // Store the entry to the database
        if (!$row->store()) {
            $this->setError($this->_db->getErrorMsg());
            return false;

        }
        return true;
    }

    public function delete()
    {
        $cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
        $row =& $this->getTable('seasons');

        foreach($cids as $cid) {
        
        	// delete only seasons w/o matches
        	$query = 'SELECT * FROM #__md_matches WHERE MatchSeasonID='.$cid;
        	$number = $this->_getListCount($query);
        	
        	if ($number != 0){
        		return false;
        	} else { 
	            if (!$row->delete( $cid )) {
	                $this->setError( $row->_db->getErrorMsg() );
	                return false;
	            }
        	}
        }

        return true;
    }

    /**
     * publish season
     */
    public function publish()
    {
        $cids = implode(',',JRequest::getVar( 'cid', array(0), 'post', 'array' ));
        //$row =& $this->getTable('seasons');
		
			$query = 'UPDATE #__md_seasons '.' SET SeasonPublish = 1 WHERE SeasonID in ('.$cids.')';
			$this->_db->setQuery( $query );
			$result = $this->_db->loadResult();
        
        if ($result){
        $message->msg = 'MD_OPERATION_SELECTPUBLISHED';
        $message->type = 'message';
        }

        return $message;
    }

    /**
     * unpublish season
     */
    public function unpublish()
    {
        $cids = implode(',',JRequest::getVar( 'cid', array(0), 'post', 'array' ));
        //$row =& $this->getTable('seasons');
		
			$query = 'UPDATE #__md_seasons '.' SET SeasonPublish = 0 WHERE SeasonDefault = 0 AND SeasonID in ('.$cids.')';
			$this->_db->setQuery( $query );
			$result = $this->_db->loadResult();
        
        if ($result){
        $message->msg = 'MD_OPERATION_SELECTUNPUBLISHED';
        $message->type = 'message';
        }

        return $message;
    }
    
    
    /**
     * toggle publish / unpublish season
     */
    public function togglePublish()
    {
        $cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
        //$row =& $this->getTable('seasons');
		
        foreach($cids as $cid) {
        	
        	$seasondata =& $this->getData();

            if ($seasondata->SeasonPublish == 1) {
				$query = 'UPDATE #__md_seasons '.' SET SeasonPublish = 0 WHERE SeasonID = '.$cid;
		        $message->msg = 'MD_OPERATION_UNPUBLISHED';
        		$message->type = 'message';
            } else {
				$query = 'UPDATE #__md_seasons '.' SET SeasonPublish = 1 WHERE SeasonID = '.$cid;
		        $message->msg = 'MD_OPERATION_PUBLISHED';
        		$message->type = 'message';
            }
			echo 'query '.$query.'<br>';
			$this->_db->setQuery( $query );
			$result = $this->_db->loadResult();
        }
        if ($result) {
        	return $message;
        }
    }
    
    
    /**
     * set default season
     */
    public function setDefault()
    {
		$cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );
		
		//only 1 default season possible
		if (count($cids) != 1) {
			$this->setError('MD_ERRONEDEFAULT');
			return false;
		} else {
			$seasondata =& $this->getData();
			
			$query = 'UPDATE #__md_seasons '.' SET SeasonDefault = 0';
			$this->_db->setQuery( $query );
			$this->_db->loadResult();

			$query = 'UPDATE #__md_seasons '.' SET SeasonDefault = 1, SeasonPublish = 1 WHERE SeasonID = '.$this->_id;
			$this->_db->setQuery( $query );
			$this->_db->loadResult();
		}
    }
}
?>